//
//  Search.m
//  EFExtend
//
//  Created by mac on 15/7/30.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "Search.h"


@interface Search ()<UISearchBarDelegate>

@property (nonatomic,strong) HMAMapPlaceSearchRequest *request;
@property (nonatomic,strong) NSMutableArray *pois;
@property (nonatomic,strong)UISearchBar *searchBar;
@property (nonatomic,copy) NSString *searchKey;

@end

@implementation Search

- (void)dealloc
{
    self.request = nil;
    self.pois = nil;
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (UISearchBar *)searchBar{
    if (_searchBar==nil) {
        _searchBar = [[UISearchBar alloc]init];
        _searchBar.placeholder = @"\"城市\":\"关键字\"";
        _searchBar.delegate = self;
        [_searchBar clearBackBar];
        _searchBar.returnKeyType = UIReturnKeySearch;
    }
    return _searchBar;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    [self customNavTitleView:self.searchBar];
    
    [self observeNotification:[HMMapSearchAPI MapSearchReturn]];
    
    self.searchBar.text = @"福州:加油站";
    
    self.pois = [NSMutableArray array];
    self.searchKey = @"福州:加油站";

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSArray *aa = [self.searchKey componentsSeparatedByString:@":"];
        self.request = [[HMMapSearchAPI sharedInstance] searchPOIKeywords:aa.count>=2?[aa lastObject]:[aa firstObject] inCities:aa.count>=2?[aa subarrayWithRange:NSMakeRange(0, aa.count-1)]:@[@"福州"]];
    }];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"listCell"];
    self.tableView.mj_footer.hidden = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [[HMMapSearchAPI sharedInstance] searchPlace:self.request];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [HMUIKeyboard hideKeyboard];
    
    self.searchKey = self.searchBar.text;
    
    if (self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer endRefreshing];
    }
    
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    }
    
    [self.tableView.mj_header beginRefreshing];
}


ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }
    }
}


ON_NOTIFI_MapSearchAPI(noti){
    if (noti.object) {
        if (self.request!=[noti.object valueForKey:MAPSEARCHKEY_REQUEST]) {
            return;
        }
        
        if (self.tableView.mj_header.isRefreshing) {
            [self.pois removeAllObjects];
            [self.tableView.mj_header endRefreshing];
        }else if (self.tableView.mj_footer.isRefreshing) {
            [self.tableView.mj_footer endRefreshing];
        }
        AMapPlaceSearchResponse *response = [noti.object valueForKey:MAPSEARCHKEY_RESPONSE];
        [self.pois addObjectsFromArray:response.pois];
        if (self.pois.count<response.count) {
            self.request.page += 1;
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
    }
}


#pragma  mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.pois.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat height = [tableView fd_heightForCellWithIdentifier:@"listCell" configuration:^(id cell) {
        
    }];
    
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"listCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    AMapPOI *poi = [self.pois safeObjectAtIndex:indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@.%@",@(indexPath.row),poi.name];
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ tel:%@\n%@",poi.location,poi.tel,poi.address];
    cell.detailTextLabel.numberOfLines = 0;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
